package com.beer.beerfriendfinder.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MUserModel extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "gcms";

	// Contacts table name
	private static final String TABLE_USERS = "gcm_user";

	// Contacts Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_EMAIL = "email";
	private static final String KEY_PH_NO = "phoneNumber";
	private static final String KEY_POS = "position";
	private static final String KEY_REGID = "regId";
	private static final String KEY_BIRTH = "birthday";
	private static final String KEY_CREATEDAY = "created_at";
	
	
	

	public MUserModel(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
				+ KEY_ID + " INTEGER PRIMARY KEY," 
				+ KEY_EMAIL + " TEXT,"
				+ KEY_PH_NO + " TEXT,"
				+ KEY_POS 	+ " TEXT,"
				+ KEY_REGID + " TEXT,"
				+ KEY_BIRTH + " TEXT,"
				+ KEY_CREATEDAY + " TEXT"
				+ ")";
		db.execSQL(CREATE_CONTACTS_TABLE);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
 
        // Create tables again
        onCreate(db);
	}
	
	public void addNewUser(Users users){
		SQLiteDatabase db = this.getWritableDatabase();
		 
	    ContentValues values = new ContentValues();
	    values.put(KEY_EMAIL, users.getEmail()); // User Name
	    values.put(KEY_PH_NO, users.getPhoneNumber()); // User Phone Number
	 
	    // Inserting Row
	    db.insert(TABLE_USERS, null, values);
	    db.close(); // Closing database connection		
	}
	
	// Getting single contact
	public Users getUser(int id) {
	    SQLiteDatabase db = this.getReadableDatabase();
	 
	    Cursor cursor = db.query(TABLE_USERS, new String[] { KEY_ID,
	            KEY_EMAIL, KEY_PH_NO }, KEY_ID + "=?",
	            new String[] { String.valueOf(id) }, null, null, null, null);
	    if (cursor != null)
	        cursor.moveToFirst();
	 
	    Users contact = new Users(Integer.parseInt(cursor.getString(0)),
	            cursor.getString(1));
	    // return contact
	    return contact;
	}

}
